package com.dao;

import com.pojo.Emp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class EmpDao extends BaseDao<Emp>{
    public Emp rowMapper(ResultSet rs){
        Emp emp = new Emp();
        try {
            emp.setEmpno(rs.getInt("empno"));
            emp.setEname(rs.getString("ename"));
            emp.setHiredate(rs.getString("hiredate"));
            emp.setSal(rs.getDouble("sal"));
            emp.setDeptno(rs.getInt("deptno"));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


        return emp;
    }
    public Emp searchByEmpno(int empno){
        String sql = "select";
        Object[] arr = {empno};
        return this.find(sql,arr);
    }

    public int insertEmp(Emp emp) {
        String sql = "insert into emp(empno,ename,hiredate,sal,deptno)values(?,?,?,?,?)";
        Object[] params = {emp.getEmpno(),
                emp.getEname(),
                emp.getHiredate(),
                emp.getSal(),
                emp.getDeptno()};
        if (this.update(sql, params)) {
            return 1;

        } else {
            return 0;
        }
    }

    public List<Emp> searchEmpno() {
        String sql = "select * from emp";
        return this.findMultiple(sql,null);
    }

    public int delete(int id) {
        String sql = "delete from emp where empno = ?";
        Object[] arr = {id};
        boolean re= update(sql,arr);
        if (re)
            return 1;
        else return 0;
    }

    public int updateEmp(Emp emp) {
        String sql = "update emp set ename = ?,hiredate = ? ,sal = ?,deptno = ? where" +
                "empno = ?";
        Object[] arr = {emp.getEname(),emp.getHiredate(),
                emp.getSal(),
                emp.getDeptno(),emp.getEmpno()};
        boolean re= update(sql,arr);
        if (re)
            return 1;
        else return 0;
    }
}
